<?php
	error_reporting(E_ALL || ~E_NOTICE);
	$controlerpagenum = 1;
//	echo "ok";
//	file_put_contents("test.log", "ok......\n", FILE_APPEND);
//    public function dwgetcontroler()
//    {
	$pagestart=1;
	$pageend=1;
	$i=1;
	$j=1;   
	$s='';
	$sqlstr='';
	$blank=false;
	$accesstab='';
	$dbResult=array();
	$rowsnum=array();
	if($_POST['page'])
	{
		$controlerpagenum = $_POST['page'];
	}
	else
	{
		$controlerpagenum = 1;
	}

	$myconn=@mysql_connect("localhost","root","root");
	if(!$myconn)
	{
		die('Could not connect: '.mysql_error());
	}
	$db_selected=mysql_select_db("dwaccessDB",$myconn);
	if($db_selected)
	{
		$strSql="select COUNT(*) from accessrecord;";
		$result=mysql_query($strSql,$myconn);	        
		$rowsnum = mysql_fetch_array($result);
//	        @file_put_contents("test.log", "rowsnum[0]:".$rowsnum[0]."\n", FILE_APPEND);
		if($rowsnum[0]==0)
		{
			$blank = true;
		}
		mysql_free_result($result);
	}else
	{
		die("Can't use database dwaccessDB : ".mysql_error());
		$string = "Can't use database : ".mysql_error();
//            file_put_contents("test.log", $string, FILE_APPEND);
	}		
	
	$pagemax=1;
	$pagemax = (($rowsnum[0]/10.0)-(int)($rowsnum[0]/10.0))>0.?(int)($rowsnum[0]/10.0)+1:(int)($rowsnum[0]/10.0);
	if($pagemax<1) $pagemax=1;
	$string = "pagemax=".$pagemax."\n";
//	    file_put_contents("test.log", $string, FILE_APPEND);
	if($controlerpagenum>$pagemax) $controlerpagenum=$pagemax;
	$pagestart=10*$controlerpagenum-10;
	$pageend=10;

	//如果请求的当前页是最后一页，则返回的条目数取余数，否则固定为10；
	if($controlerpagenum == $pagemax)
	{
		if(($rowsnum[0]%10)==0)
		{
			$rowsnum[0] = 10;
		}else
		{
			$rowsnum[0] = $rowsnum[0]%10;
		}
	}
	else
	{
		$rowsnum[0] = 10;
	}

	//$strSql="select tkid,cardnum,cardlen,controlerNum,gatenum,CreatedTime from accessrecord limit ".$pagestart.','.$pageend.';';
	$strSql="select tkid,cardnum,gatenum,controlerNum,CreatedTime,accessstat,entrance from accessrecord ORDER BY CreatedTime DESC limit ".$pagestart.','.$pageend.';';
//        file_put_contents("test.log", "strSql =".$strSql, FILE_APPEND);
	$result=mysql_query($strSql,$myconn);		
//	    @file_put_contents("test.log", "rowsnum[0] =".$rowsnum[0], FILE_APPEND);	    
//		for(i=1;i<=$rowsnum[0];i++)
	while($dbResult = mysql_fetch_array($result))
	{	        
		for($j=1;$j<=7;$j++)
		{			   
			if($i==1 && $j==1)
			{			        
				$accesstab = "{\"nrows\":\"".$rowsnum[0]."\",\"ncols\":\"7\",\"pageMaxium\":\"".$pagemax."\",\"controlerpagenum\":\"".$controlerpagenum."\",\"s0".$i.'_'.($j-1)."\":\"".$dbResult[($j-1)].'"';						
			}else
			{
				$accesstab = $accesstab.",\"s0".$i.'_'.($j-1)."\":\"".$dbResult[($j-1)].'"';
			}
		}
		$i++;			
	}
//    }	
	// echo $pagemax;
	// echo "\r\n";
	// echo $rowsnum;
	// echo "\r\n";
	if(!$blank)
	{
		$accesstab = $accesstab."}";
	}
	else
	{
		$accesstab = "{\"nrows\":\"0\",\"ncols\":\"7\",\"pageMaxium\":\"0\",\"controlerpagenum\":\"0\"}";
	}	
	
	mysql_free_result($result);
	mysql_close($myconn);	
	echo $accesstab;
//		@file_put_contents("test.log", "\n accesstab =".$accesstab."\n", FILE_APPEND);
?>